#!/bin/sh

#
# /etc/init.d/logrotate - Minimal Log Rotator for /var/log/{messages,*.{log,err}}
#

PREFIX="logrotate: "
LOGDIR="/var/log"
MAXFILES="8"
PATTERNS="*.err *.log messages"

my_exit(){
	echo "${PREFIX}${1}"
	exit $2
}

logrotate(){
	cd "${LOGDIR}"
	# rotate backups for log files
	for filename in ${PATTERNS}; do
		[ -e "${filename}" ] &&
		echo "${PREFIX}rotation for ${filename} ... " &&
		for i in `seq $(( MAXFILES - 1 )) -1 1` ; do
			if [ -e "${filename}.${i}" ]
			then
				j=$(( i + 1 ))
				mv -f -- "${filename}.${i}" "${filename}.${j}"
			fi
		done &&
		mv -f -- "${filename}" "${filename}.1"
	done
}

#
# Main
#

[ -d "$LOGDIR" ] || my_exit "Logdir $LOGDIR missing" 1

case $1 in

	start|stop)
		echo "${PREFIX}rotating in $LOGDIR"
		logrotate
		;;
	*)
		echo "Usage: $0 [start|stop]"
		;;
esac
